package com.chengxiaoxiao.ai_demo.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chengxiaoxiao.ai_demo.entity.OriginalMessage;
import com.chengxiaoxiao.ai_demo.mapper.OriginalMessageMapper;
import com.chengxiaoxiao.ai_demo.service.OriginalMessageService;
import com.chengxiaoxiao.ai_demo.vo.form.ChatForm;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import org.springframework.stereotype.Service;

import java.util.List;

import static dev.langchain4j.data.message.ChatMessageSerializer.messageToJson;


/**
 * originalMessage 服务实现类
 *
 * @Description:
 * @Author: Cheng Xiaoxiao
 * @Date: 2025-03-20 16:24:09
 */
@Service
public class OriginalMessageServiceImpl extends ServiceImpl<OriginalMessageMapper, OriginalMessage> implements OriginalMessageService {
    /**
     * 获取originalMessage详情
     *
     * @param id ID
     * @return 单个originalMessage详情
     */
    @Override
    public OriginalMessage detailById(String id) {
        return getById(id);
    }

    /**
     * 增加originalMessage
     *
     * @param originalMessage originalMessage对象
     * @return 增加后的数据
     */
    @Override
    public OriginalMessage saveUserMessage(OriginalMessage originalMessage) {
        this.baseMapper.insert(originalMessage);
        return originalMessage;
    }

    /**
     * 修改originalMessage
     *
     * @param originalMessage originalMessage对象
     * @return 修改后的数据
     */
    @Override
    public OriginalMessage updateOriginalMessageById(OriginalMessage originalMessage) {
        this.baseMapper.updateById(originalMessage);
        return originalMessage;
    }

    /**
     * 根据ID删除
     *
     * @param id ID
     */
    @Override
    public void deleteById(String id) {
        removeById(id);
    }

    /**
     * 根据会话ID查询原始消息
     *
     * @param conversationId 会话ID
     * @return 原始消息
     */
    @Override
    public List<OriginalMessage> listByConversationId(String conversationId) {
        return baseMapper.selectList(new LambdaQueryWrapper<OriginalMessage>().eq(OriginalMessage::getConversationId, conversationId));
    }

    /**
     * 保存用户消息
     *
     * @param id       会话ID
     * @param chatForm 用户表单
     */
    @Override
    public void saveUserMessage(String id, ChatForm chatForm) {
        UserMessage userMessage = new UserMessage(chatForm.getText());

        OriginalMessage originalMessage = new OriginalMessage();
        originalMessage.setConversationId(id);
        originalMessage.setMessage(messageToJson(userMessage));
        originalMessage.setContext(JSON.toJSONString(chatForm.getContext()));

        this.save(originalMessage);
    }

    /**
     * 保存AI消息
     *
     * @param id   会话ID
     * @param text 消息
     */
    @Override
    public void saveAiMessage(String id, String text) {
        AiMessage aiMessage = new AiMessage(text);

        OriginalMessage originalMessage = new OriginalMessage();
        originalMessage.setConversationId(id);
        originalMessage.setMessage(messageToJson(aiMessage));

        this.save(originalMessage);
    }
}